var tekN=0;
function vseOk(mes){
	$("#rezDiv").fadeTo(0, 1);
	$("#rezDiv").text(mes);
	$("#rezDiv").fadeTo(2000, 0);
}

function resetTable(){
	$("#need tr:gt(0)").remove();
};

function selectItem(li) {
	$.getJSON("/controllers/getFormData.php", {good: li.selectValue, chto:"cat"}, function(json) {
		$("#cat").val(json.cat);
	});		
}

$(document).ready(function(){

$('#addRow').click(function() { 
	if ($("#item").val()!="" && $("#cat").val()!="" && $("#col").val()!=""){

		$.getJSON("/controllers/addNeed.php", $("#needItem").formSerialize(), function(json) {
				$("#need tbody").prepend(	"<tr>" +
						"<td>"+json.id+"</td>" +
						"<td>"+$("#item").val()+"</td>" +
						"<td>"+$("#cat").val()+"</td>" +
						"<td>"+$("#col").val()+"</td>" +
						"<td>"+$("#srok").val()+"</td>" +
						"<td>"+$("#date").val()+"</td>" +
						"<td>"+$("#prim").val()+"</td>" +
						"<td><input type='checkbox' class='inBaseNeed' /></td>" +
						"<td><input type='button' class='delete' value='delete' /></td>" +
					"</tr>");
				$("#needItem").resetForm();
				vseOk(json.mes);
				
		});		
	}
	return false;
});

$(".inBaseNeed").live("click",function() {
	$.get("/controllers/setHave.php", {need: $(this).val(), check: this.checked}, function(data){
		vseOk(data);
	});
});

$("#item").autocomplete("/controllers/autocomplete.php", {
	delay:100,
	minChars:1,
	matchSubset:1,
	matchContains:1,
	maxItemsToShow:10,
	onItemSelect:selectItem,
	extraParams:{tab:"goods", field:"item"}
}); 

$("#cat").autocomplete("/controllers/autocomplete.php", {
	delay:200,
	minChars:1,
	matchSubset:1,
	matchContains:1,
	maxItemsToShow:10,
	extraParams:{tab:"categ", field:"cat"}
}); 

$("#srok").autocomplete("/controllers/autocomplete.php", {
	delay:100,
	minChars:1,
	matchSubset:1,
	matchContains:1,
	maxItemsToShow:10,
	extraParams:{tab:"srok", field:"srok"}
}); 


$(".delete").live("click",function() {
	var parent = $(this).parent().parent();
	delId=parent.find("td:first").text();
	$.get("/controllers/delNeed.php", {need: delId}, function(data){
		vseOk(data);
	});
	parent.remove();
    return false;
});

});

